package com.gancp.common.security;


import com.gancp.common.security.Base64Util;

import javax.crypto.Cipher;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * Created by GCP on 2017/11/28.
 */
public class RSAUtil {

    /****定义Rsa算法常量**/
    public static final String KEY_ALGORITHM = "RSA";
    /***私钥**/
    private static String PRIVATE_KEY = "";
    /***公钥*/
    public static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOO7k/4brmeBeToVrwY7O1 KV+Uja8E601x3q6dSp2pBeLO7yc3Wz/o03qCU2bdpV0WOYqRxSzlPtimku6k iDX9g+kLQH7dkbMlsd3/C4lMpQ35fq3CtUyxJyMNPCVZNnHKfyCODPnkPFuI OR1pZPuF/KOQcdOBv0WO0LzUXbpNeQIDAQAB";

    public static final String CHARSET_NAME = "UTF-8";

    /***私有化构造器 安全访问**/
    private RSAUtil() {
    }

    public static void main(String[] args) throws Exception {
/*        // 获取秘钥构造器
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( KEY_ALGORITHM );
        keyPairGenerator.initialize( 1024 );
        // 获取密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        // 公钥
        PublicKey publicKey = keyPair.getPublic();
        // 私钥
        PrivateKey privateKey = keyPair.getPrivate();
        System.out.println( Base64Util.encode( publicKey.getEncoded() ) );
        System.out.println( Base64Util.encode( privateKey.getEncoded() ) );*/
        String data = "面对疾风吧";
        byte[] enResult = encryptByPublicKey( data.getBytes( CHARSET_NAME ), PUBLIC_KEY );
        System.out.println( enResult.toString() );
        byte[] result = decryptByPrivateKey( enResult );
        System.out.println( new java.lang.String( result, CHARSET_NAME ) );

    }

    /***获取密钥***/
    public static void convert() throws Exception {
        byte[] data = null;
        try {
            InputStream is = RSAUtil.class.getResourceAsStream( "/enc_pri" );
            int length = is.available();
            data = new byte[length];
            is.read( data );
        } catch (IOException e) {
            e.printStackTrace();
        }
        String dataStr = new String( data );
        PRIVATE_KEY = dataStr;
        if (PRIVATE_KEY == null) {
            throw new Exception( "解析秘钥失败！" );
        }
    }

    /***私钥解密****/
    public static byte[] decryptByPrivateKey(byte[] data) throws Exception {
        convert();
        byte[] keyBytes = Base64Util.decode( PRIVATE_KEY );
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( keyBytes );
        KeyFactory keyFactory = KeyFactory.getInstance( KEY_ALGORITHM );
        Key privateKey = keyFactory.generatePrivate( pkcs8EncodedKeySpec );
        Cipher cipher = Cipher.getInstance( keyFactory.getAlgorithm() );
        cipher.init( Cipher.DECRYPT_MODE, privateKey );
        return cipher.doFinal( data );
    }

    /***公钥加密**/
    public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {
        byte[] keyBytes = Base64Util.decode( key );
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( keyBytes );
        KeyFactory keyFactory = KeyFactory.getInstance( KEY_ALGORITHM );
        Key publicKey = keyFactory.generatePublic( x509EncodedKeySpec );
        Cipher cipher = Cipher.getInstance( keyFactory.getAlgorithm() );
        cipher.init( cipher.ENCRYPT_MODE, publicKey );
        return cipher.doFinal( data );

    }
}
